package Servlet;

import com.cyy.bean.salesman;
import com.cyy.bean.user;
import com.cyy.dao.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/showMyInfo")
public class showMyInfoServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
//        展示用户名
        String userName=request.getSession().getAttribute("name").toString();
        int identity=Integer.parseInt(request.getSession().getAttribute("identity").toString());
        loginRecordDao loginRecordDao =null;
        ArrayList<String> loginRecords = null;
        //        登录记录
        loginRecordDao = new loginRecordDao();
        try {
            loginRecords = loginRecordDao.selectAllByName(userName, identity);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        if(identity==0) {
            user user1 = null;
//          获得用户的基本信息
            userDao userDao = new userDao();
            try {
                user1=userDao.selectAll(userName);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            assert user1 != null;
            request.setAttribute("pwd", user1.getPwd());
            request.setAttribute("email", user1.getEmail());
            request.setAttribute("registerTime", user1.getRegisterTime());
            request.setAttribute("address",user1.getAddress());
            request.setAttribute("phone",user1.getPhone());
        }
        else
        {
            System.out.println("进入售货员的部分");
            salesman salesman1=null;
//           获得该用户的基本信息
            salesmanDao salesmanDao = new salesmanDao();
            try {
                salesman1=salesmanDao.selectAll(userName);
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            assert salesman1 != null;
            request.setAttribute("pwd", salesman1.getPwd());
            request.setAttribute("email", salesman1.getEmail());
            request.setAttribute("registerTime", salesman1.getRegisterTime());
        }
        request.setAttribute("loginRecords", loginRecords);
        request.getRequestDispatcher("myInfo.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
